import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class java06 {

    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>();
        try {
            addStudent(students, 3);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            return;
        }
        printStudent(students);
        System.out.println("幸运儿是 " + randomStudent(students));
    }

    public static void addStudent(ArrayList<Student> s, int n) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < n; i++) {
            Student a = new Student();
            System.out.println("输入");
            a.setName(scanner.next());
            int age = scanner.nextInt();
            try {
                a.setAge(age);
            } catch (IllegalArgumentException e) {
                System.out.println(e.getMessage());
                i--;  // 输入错误，重新输入当前学生信息
                continue;
            }
            a.setId(i);
            s.add(a);
        }
    }

    public static void printStudent(ArrayList<Student> s) {
        for (int i = 0; i < s.size(); i++) {
            s.get(i).getStudent();
        }
    }

    public static String randomStudent(ArrayList<Student> s) {
        if (s.isEmpty()) {
            throw new IllegalArgumentException("学生列表为空，无法选取");
        }
        int index = new Random().nextInt(s.size());
        return s.get(index).getName();
    }
}

class Student {
    private int age;
    private String name;
    private int id;

    public void setAge(int age) {
        if (age > 18 && age < 24) {
            this.age = age;
        } else {
            throw new IllegalArgumentException("年龄必须在 19 到 23 之间");
        }
    }

    public int getAge() {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public void getStudent() {
        // 假设这里打印学生的信息
        System.out.println("Name: " + name + ", Age: " + age + ", ID: " + id);
    }
}
